<template>
    <div class="login-page">
        <div class="header">
            <div class="top">
                <h4 v-if="login">登录</h4>
                <h4 v-else>注册</h4>
                <!-- @click="$router.go(-1)" -->
                <span class="cancel" @click="$router.go(-1)">取消</span>
        </div>
        </div>
        <div class="login_inp2" v-if="login">
            <div class="f">
                <span>手机号</span>
                <input type="text" placeholder="请输入手机号" id="username" v-model="loginForm.phone">
            </div>
            <div class="s">
                <span>密&nbsp;&nbsp;&nbsp;码</span>
                <input type="password" placeholder="请输入密码" id="password" v-model="loginForm.password">
            </div>
            <div class="txt" @click="checked=!checked">
                 <img v-if="checked" src="../assets/images/圈.png" alt="">
                <img v-if="!checked" src="../assets/images/圈钩.png" alt="">
                <span>阅读并同意<i>《用户协议》《隐私协议》</i></span>
            </div>
            <button :class="['lo', {'lg':loginForm.phone&&loginForm.password !=null}]" @click="submit">立即登录</button>
        </div>
        <div class="login_inp2" v-if="!login">
            <div class="f">
                <span>手机号</span>
                <input type="text" placeholder="请输入手机号" id="userphone" v-model="registerForm.phone">
            </div>
            <div class="f">
                <span>用户名</span>
                <input type="text" placeholder="请输入用户名" id="username" v-model="registerForm.username">
            </div>
            <div class="s">
                <span>密&nbsp;&nbsp;&nbsp;码</span>
                <input type="password" placeholder="请输入密码" id="password" v-model="registerForm.password">
            </div>
            <div class="s2">
                <span>确认密码</span>
                <input type="password" placeholder="确认密码" id="password2" v-model="registerForm.password2">
            </div>
            <!-- <div class="txt" @click="checked=!checked">
                 <img v-if="checked" src="../assets/images/圈.png" alt="">
                <img v-if="!checked" src="../assets/images/圈钩.png" alt="">
                <span>阅读并同意<i>《用户协议》《隐私协议》</i></span>
            </div> -->
            <button :class="['lo', {'lg':registerForm.phone&&registerForm.password !=null}]" @click="register">立即注册</button>
        </div>
        <div class="wechat">
            <!-- <div class="img">
                <img src="../assets/images/wechat.png" alt="">
            </div> -->
            <div class="register" @click="login=false">还没有账号? 那就来<span>注册</span>一个吧</div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                login: true,
                checked:false,
                userPhone:null,
                userPassword:null,
                loginForm:{
                    phone:'',
                    password:''
                },
                registerForm:{
                    username:'',
                    phone:'',
                    password:'',
                    password2:'',
                }
            };
        },
        methods: {
            selecty(){
                this.selected = true
                window.localStorage.setItem("state",this.selected)
            },
            selectm(){
                this.selected = false
                window.localStorage.setItem("state",this.selected)
            },
            submit(){
                // 验证手机号码与密码
                
                // 1.验证手机号码
                let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                // 2.验证密码
                let pass = /^[a-zA-Z0-9]\w{8,17}$/;
 
                if(!reg.test(this.loginForm.phone) || !pass.test(this.loginForm.password) ){//不正确处理
                    this.$toast("请输入正确手机号码与密码");
                    return;
                }

                //用axios向nodejs搭建的后台服务器发送请求
                this.$axios.post(this.HOST+'/getUserInfo',this.loginForm).then((res)=>{
                    console.log(res);
                    if (res.data.data != null) {
                        this.$toast("登陆成功");
                        window.localStorage.setItem("token",res.data.token)
                        let userObj = {
                            id:res.data.data[0].id,
                            username:res.data.data[0].username,
                            imageid:"https://pic.ecook.cn/web/8877665544.jpg!s1",
                            followsCount:0,
                            fansCount:0,
                            specialities:[],
                            profile:"热爱生活",
                            sex:1,
                            level:99,
                            isMember:true,
                            title:res.data.data[0].username,
                            onlineTeachCount:0,
                            recipeCount:0,
                            talkCount:0,
                            collectionSortCount:0,
                            articleCount:0,
                        }
                        window.localStorage.setItem("userinfo",JSON.stringify(userObj))
                        window.localStorage.setItem("userId",123564)
                        this.$router.go(-1)

                    }else{
                        this.$toast("请输入正确手机号码与密码");
                    }

                })
            },
            //注册
            register(){
                // 验证手机号码与密码
                
                // 1.验证手机号码
                let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                // 2.验证密码
                let pass = /^[a-zA-Z0-9]\w{8,17}$/;
 
                if(!reg.test(this.registerForm.phone) || !pass.test(this.registerForm.password) ){//不正确处理
                    this.$toast("请输入正确手机号码与密码");
                    return;
                }
                if (this.registerForm.password2!=this.registerForm.password) {
                    this.$toast("两次输入的密码不一致");
                }
                if (this.registerForm.password2=='') {
                    this.$toast("请确认密码");
                }
                if(this.registerForm.username==''){
                    this.$toast("输入用户名");
                }
                //用axios向nodejs搭建的后台服务器发送请求
                this.$axios.post(this.HOST+'/registerPost',this.registerForm).then((res)=>{
                    // console.log(res);
                    if (res.data.status=='OK') {
                        this.login = true
                    }else{
                        this.$toast("该手机号已被注册");
                    }
                })
            },
            
        },
    }
</script>

<style lang="less" scoped>
.header {
    width: 100%;
    height: 45px;
    border-bottom: 1px solid #ccc;
    position: relative;

    .top {
        width: 100%;
        text-align: center;

        &>h4 {
            font-weight: 600;
            font-size: 17px;
            padding-top: 12px;
        }

        .cancel {
            position: absolute;
            top: 10px;
            left: 10px;
            font-size: 15px;
            color: #999;
        }
    }

    .tap {
        width: 80%;
        height: 30px;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        .left {
             position: relative;
            .yz {
                color: #999;
                font-weight: 600;
                font-size: 15px;
                letter-spacing: 1.2px;
            }

        }

        .right {
             position: relative;
            .mima {
                font-weight: 600;
                font-size: 15px;
                letter-spacing: 1.2px;
                color: #999;
            }
        }
        .active{  
           .yz{
            color: #fed74d;
           }
           .mima{
            color: #fed74d;
           }
        }
    }
}

.login_inp2,
.login_inp1 {
    width: 90%;
    margin: 0 auto;
    margin-top: 30px;
    text-align: center;

    .f {
        margin-bottom: 30px;
        display: flex;
        align-items: center;

        &>span {
            font-weight: 600;
            font-size: 15px;
        }

        &>input {
            margin-left: 10px;
            font-size: 14px;
            color: #000;
            border: none;
            outline: none;
            background-color: #fff;
        }
    }

    .s2,
    .s {
        position: relative;
        display: flex;
        align-items: center;

        &>span {
            font-weight: 600;
            font-size: 15px;
        }

        &>input {
            margin-left: 12px;
            font-size: 14px;
            color: #000;
            border: none;
            outline: none;
            background-color: #fff;
        }

        &>a {
            font-size: 14px;
            padding: 8px;
            background-color: #f1f1f1;
            border-radius: 8px;
            color: #fed74d;
            font-weight: 600;
            position: absolute;
            right: 0;
        }
    }
    .s{
        margin-bottom: 30px;
    }

    .txt {
        margin-top: 35px;
        display: flex;
        align-items: center;

        &>img {
            width: 15px;
            height: 15px;
            display: block;
            margin-right: 5px;
        }

        &>span {
            font-size: 13px;

            &>i {
                color: #fed74d;
                font-weight: 600;
            }
        }
    }

    .lo {
        display: block;
        width: 90%;
        margin: 0 auto;
        margin-top: 30px;
        padding: 13px;
        border-radius: 8px;
        background-color: #dddddd;
        font-size: 14px;
        color: #fff;
        font-weight: 600;
        border: none;
    }
    .lg{
        background-color: #fed74d;
    }
}

.wechat {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: absolute;
    bottom: 30px;
    left: 0;
    right: 0;
    .img {
        width: 35px;
        height: 35px;
        border: 1px solid #ccc;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 5px;
        &>img{
            width: 30px;
            height: 30px;
            display: block;
        }
    }
    .register{
        font-size: 14px;
        &>span{
        font-size: 16px;
        font-weight: 700;
        color: #fed74d;
        letter-spacing: 1.2px;
        padding: 0 2px;
    }
    }
}
</style>